package com.example.auth.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.auth.domain.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
* @author CETC
* @description 针对表【user】的数据库操作Mapper
* @createDate 2025-10-03 18:06:35
* @Entity com.example.auth.domain.User
*/
public interface UserMapper extends BaseMapper<User> {

    @Select("SELECT * FROM ${tableName} WHERE  username=#{userName} and is_deleted='0'")
    public User getUserByUsername(@Param("tableName")String tableName, @Param("userName")String userName);

    @Select("SELECT * FROM ${tableName} WHERE  uuid=#{uuid} and is_deleted='0'")
    public User getUserByUserId(@Param("tableName")String tableName,@Param("uuid")String uuid);

    @Select("SELECT count(1) FROM ${tableName} WHERE  is_deleted='0'")
    public Integer getUserCountByTable(@Param("tableName")String tableName);

    @Update("UPDATE ${tableName} SET is_deleted='1',update_time = NOW() WHERE uuid = #{uuid}")
    int deleteData(@Param("tableName")String tableName,@Param("uuid")String uuid);

    @Update("<script>" +
            "   UPDATE ${tableName} " +
            "       SET is_deleted = '1', " +
            "        update_time = NOW()  " +
            "   WHERE uuid IN " +
            "       <foreach collection='ids' item='id' open='(' close=')' separator=','>" +
            "           #{id}" +
            "       </foreach>" +
            "</script>")
    int batchDeleteUserByIds(@Param("tableName")String tableName,@Param("ids") List<String> ids);

    @Select({"<script>" +
            "   SELECT * FROM ( ${tableName} ) as a " +
            "       <where>" +
            "           <if test='userName != null '>" +
            "                userName = #{userName}  and is_deleted='0' order by create_time desc" +
            "           </if>" +
            "           <if test='userName == null '>" +
            "                is_deleted='0' order by create_time desc" +
            "           </if>" +
            "       </where>"+
            "</script>"})
    public IPage<User> selectUserByPage(Page<User> page, @Param("tableName")String tableName,@Param("userName")String userName);








}




